package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingService.class)
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Override
    public void importOrderSettings(List<OrderSetting> orderSettings) {

        for (OrderSetting orderSetting : orderSettings) {
            //通过日期查询指定的那天是否已经在设置过可预约人数
            Integer count = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());
            if (count>0){
                //如果已经设置过，就执行更新
                orderSettingMapper.updateNumberByOrderDate(orderSetting);
            }else{
                //如果没有设置过，就执行新增
                orderSettingMapper.add(orderSetting);

            }
        }

    }

    /**
     * 日历展示可预约数据
     * @param date
     * @return
     */
   /* @Override
    public List<Map> findOrderSettingByMonth(String date) {

        //定义查询的每个月的起始值
        String dateBegin = date+"-01";
        //定义查询的每个月的结束值
        String dateEnd = date+"-31";

        //通过日期查询指定月份的可预约数据
        List<OrderSetting> orderSettings = orderSettingMapper.findOrderSettingByMonth(dateBegin,dateEnd);

        //定义一个List，用来存放map
        List<Map> list = new ArrayList<>();

        //遍历每一个OrderSetting,将其重新封装为一个个的map，再将map重新封装到List中
        for (OrderSetting orderSetting : orderSettings) {

            Map<String, Object> map = new HashMap<>();
            map.put("date",orderSetting.getOrderDate().getDate());
            map.put("number",orderSetting.getNumber());
            map.put("reservations",orderSetting.getReservations());
            list.add(map);

        }

        return list;
    }*/

    @Override
    public List<Map> findOrderSettingByMonth(String date) {

        //定义查询的每个月的起始值
        String dateBegin = date+"-01";
        //定义查询的每个月的结束值
        String dateEnd = date+"-31";

        //通过日期查询指定月份的可预约数据
        List<Map> orderSettings = orderSettingMapper.findOrderSettingByMonth(dateBegin,dateEnd);

        return orderSettings;
    }

    @Override
    public void updateNumberByOrderDate(OrderSetting orderSetting) {

        //通过日期查询指定的那天是否已经在设置过可预约人数
        Integer count = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());
        if (count>0){
            //如果已经设置过，就执行更新
            orderSettingMapper.updateNumberByOrderDate(orderSetting);
        }else{
            //如果没有设置过，就执行新增
            orderSettingMapper.add(orderSetting);
        }
    }

    @Override
    public void delByDate() {
        orderSettingMapper.delByDate();
    }
}
